//
//  YZMyStocksSearchCell.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/31.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMyStocksSearchCell.h"
#import "YZMStockInfo.h"
#import "YZMyStocksService.h"
#import "YZMConfigureService.h"

#import <BlocksKit/UIControl+BlocksKit.h>

@interface YZMyStocksSearchCell()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *codeLabel;

@property (nonatomic, strong) UIButton *myStockButton;
@property (nonatomic, strong) UIView *highRiskView;

@end

@implementation YZMyStocksSearchCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createObjects];
    }
    return self;
}

- (void)setRecord:(YZMStockInfo *)record{
    
    _record = record;
    self.nameLabel.text = record.name;
    self.codeLabel.text = record.code;
    self.myStockButton.selected = [[YZMyStocksService sharedService] isStockExist:record.code];
    self.highRiskView.hidden = ![[YZMConfigureService sharedService] isInLimitStockListWithStockCode:record.code];
}

- (void)createObjects{
    
    self.nameLabel = [YZMAppStyle labelWithLargeWhite];
    [self.contentView addSubview: self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.centerY.equalTo(self.contentView);
    }];
    
    self.codeLabel = [YZMAppStyle labelWithSmallLightGray];
    [self.contentView addSubview: self.codeLabel];
    [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(10);
        make.centerY.equalTo(self.contentView);
    }];
    
    
    __weak __typeof(&*self) weakSelf = self;
    self.myStockButton = [UIButton buttonWithType: UIButtonTypeCustom];
    [self.contentView addSubview: self.myStockButton];
    [self.myStockButton setImage: [UIImage imageNamed:@"icon_stock_add_square"] forState:UIControlStateNormal];
    [self.myStockButton setImage:[UIImage imageNamed:@"icon_stock_delete_square"] forState:UIControlStateSelected];
    [self.myStockButton bk_addEventHandler:^(UIButton *sender) {
        
        if (weakSelf.operAction) {
            
            BOOL result = weakSelf.operAction(weakSelf.record, !weakSelf.myStockButton.selected);
            if (result) {
                
                sender.selected = !sender.selected;
            }
        }
        
    } forControlEvents:UIControlEventTouchUpInside];
    [self.myStockButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.centerY.equalTo(self.contentView);
    }];
    
    self.highRiskView = [UIView new];
    self.highRiskView.layer.cornerRadius = 4.0;
    self.highRiskView.layer.masksToBounds = YES;
    self.highRiskView.backgroundColor = [YZMAppStyle colorForRiseText];
    [self.contentView addSubview:self.highRiskView];
    [self.highRiskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.codeLabel.mas_right).offset(15);
        make.centerY.equalTo(self.nameLabel);
    }];
    
    UILabel *highRiskLB = [UILabel new];
    highRiskLB.text = @"高风险";
    highRiskLB.textColor = [UIColor whiteColor];
    highRiskLB.font = [YZMAppStyle smallSizeFont];
    [self.highRiskView addSubview:highRiskLB];
    [highRiskLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.highRiskView).offset(6);
        make.right.equalTo(self.highRiskView).offset(-6);
        make.top.equalTo(self.highRiskView).offset(2);
        make.bottom.equalTo(self.highRiskView).offset(-2);
    }];
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [super setHighlighted:highlighted animated:animated];
    self.highRiskView.backgroundColor = [YZMAppStyle colorForRiseText];
}

@end
